package com.icedata.sika.launcher

import com.icedata.sika.core.Sika
import com.icedata.sika.core.SikaConfig
import com.icedata.sika.core.SikaConfigNotFoundException
import com.icedata.sika.database.Column
import com.icedata.sika.database.Table
import com.icedata.sika.util.Logger
import net.mamoe.mirai.BotFactory
import net.mamoe.mirai.utils.BotConfiguration

class Launcher constructor(private val config: SikaConfig) {

    private val botConfig: BotConfiguration by lazy {
        return@lazy BotConfiguration().apply {
            // 加载设备配置
            if (config.devicePath.isNotEmpty())
                fileBasedDeviceInfo(config.devicePath)
            // 挂载网络日志
            if (!config.netLog)
                noNetworkLog()
            // 挂载bot日志
            if (!config.botLog)
                noBotLog()
            // 选用bot协议类型
            protocol = when (config.protocol) {
                "ANDROID-PAD" -> BotConfiguration.MiraiProtocol.ANDROID_PAD
                "ANDROID-WATCH" -> BotConfiguration.MiraiProtocol.ANDROID_WATCH
                else -> BotConfiguration.MiraiProtocol.ANDROID_PHONE
            }
            // 选用挤下线重连
            if (!config.autoReconnect)
                autoReconnectOnForceOffline()
        }
    }

    suspend fun launch() {
        val bot = BotFactory.newBot(
            qq = config.qq,
            password = config.password,
            configuration = botConfig
        )
        bot.login()

    }

}

suspend fun main() {
    try {
        Sika(SikaConfig()).apply {
            init()
            databaseInsert(Test().apply {
                id = 114514
                data = 1919810
            })
            println(databaseSelect<Test>(114514).data)

            close()
        }

    } catch (e: SikaConfigNotFoundException) {
        Logger.logError(e)
    }
}

/**
 * 一个简单的demo类
 */
@Table(name = "test", primaryKey = "id", database = "main")
class Test {

    @Column(name = "id")
    var id: Int = 4

    @Column(name = "data")
    var data: Int = 3
}